<?php 
require_once "config.php";
require_once 'class/class.phpmailer.php';


//$link = "http://hotwo.indisil.com/listing.php?offer_prefix=helengeli-island-offer";
//if (isset($_POST['submit'])){
    $enc_member_id='';
    $error_msg=array();
    //echo(":".$_POST["to_email"].";");
    $to=isset($_POST['to_email']) && strtolower($_POST["to_email"])!="to email here"?$_POST['to_email']:'';
    //echo "<br> to:".$to."<br>";
    if(empty($to) || !validEmail($to)){
        $return_arr["errors"]=array(array('field'=>'to_email','error'=>'Invalid To email.'));
        $return_arr["success"]= false;
        echo json_encode($return_arr);
        return ;        
        //$error_msg["to_error"]="Invalid To Email";
    }    
    
    $offer_link=isset($_POST['offer_link'])?$_POST['offer_link']:'';
    
    if(strlen(trim($offer_link))==0){
        //$error_msg["offer_error"]="Invalid Offer";
        $return_arr["errors"]=array(array('field'=>'offer_link','error'=>'Invalid Offer.'));
        $return_arr["success"]= false;
        echo json_encode($return_arr);
        return ; 
    }
    //if(empty($error_msg)){
        if (isset($_SESSION['member_id'])){
            $en_key='Oi56TCVH@#%^&*TFf78@hey@2';
            $enc = new Encryption();
            //echo "<br>member_id: ".$_SESSION['member_id'];
            $enc_member_id = $enc->encrypt($en_key, $_SESSION['member_id']);
        }  else {
            $from=  isset($_POST['from_email']) && strtolower($_POST["from_email"])!="from email here"?$_POST['from_email']:'';
            if(empty($from) || !validEmail($from)){
                //$error_msg["from_error"]="Invalid From email";
                $return_arr["errors"]=array(array('field'=>'offer_link','error'=>'Invalid Offer.'));
                $return_arr["success"]= false;
                echo json_encode($return_arr);
                return ;
            }
        }
        //echo "<br> from_error:".$error_msg["from_error"];
        //if(!isset($error_msg["from_error"])){            
            if (strlen($enc_member_id)>0){
                $offer_link.="&k=".$enc_member_id;
            }
            if (!empty($to)){
                $offer_link.="&rm=".$to;    //reffered email
            }
            $body = "<p>Hi,</p>
                <p>One of your friends has sent this link for you,<br>
                ".$offer_link."</p>
                <p>Team HWO.</p>
                ";
            //echo "<br>enc_member_id: ".$enc_member_id;
            $mail = new PHPMailer();
            //$mail->HeaderLine('Content-type', 'text/html');
            $mail->IsHTML(true);
            $mail->AddAddress($to);
            if (strlen($from)>0){
                $mail->From = $from;
            }
            $mail->Subject = "Hotel offer";
            $mail->Body = $body;
            
            if ($mail->Send()){
                //$error_msg["send_error"]="There has been an error sending the email to ".$to;
                $return_arr["success"]= true;
                echo json_encode($return_arr);	
                //echo "There has been an error sending the email to ".$to;
            }  else {
                //$error_msg["send_error"]="Your mail has been successfully sent";
                $return_arr["success"]= false;
                echo json_encode($return_arr);
                //echo "Your mail has been successfully sent";
            }    
        //}
    //}
    
//}

?>
